//
//  CommentsViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/4/27.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "CommentsViewController.h"

@interface CommentsViewController () {

    UITextView *_textView;
    
    BOOL isFirst;
}

@end

@implementation CommentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = AppBackGroundColor;
    self.commmentStr = [[NSMutableString alloc]init];
    
    if (!_commentArr) {
        _commentArr = [[NSMutableArray alloc]init];
    }
    
    float btnFrameNum = 0;
    float btnFrameTag = 0;
    
    
    
    NSArray *commentArr = @[@"不要辣",@"不要香菜",@"少放盐",@"少油",@"酒水冰镇"];
    
    UIView *bgView = [[UIView alloc]init];
    
    bgView.frame = CGRectMake(0, 64, APPWith, 100);
    
    bgView.layer.borderColor = [[UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:244.0/255.0 alpha:1]CGColor];
    
    bgView.layer.borderWidth = 1;
    
    [self.view addSubview:bgView];
    
    for (int i = 0; i < commentArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        //获取字的长度
//        CGSize size = [commentArr[i] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil].size;
        
        
        
        [btn setTitle:commentArr[i] forState:UIControlStateNormal];
        
        if (btnFrameNum+80>APPWith) {
            
            btnFrameNum = 0;
            
            btnFrameTag ++;
        }
        
//        if (size.width > 80) {
//            size.width -= 40;
//            
//            btn.titleLabel.numberOfLines = 2;
//        }
        
//        btn.frame = CGRectMake(20+btnFrameNum, 10+ btnFrameTag*50, size.width+20, 40);
        
        btn.frame = CGRectMake(10+btnFrameNum, 10+btnFrameTag*50, 80, 40);
        
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        btn.layer.cornerRadius = 10;
        
        btn.layer.borderColor = [[UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:244.0/255.0 alpha:1]CGColor];
        btn.layer.borderWidth = 1;
        
        btn.backgroundColor = [UIColor whiteColor];
        
        [bgView addSubview:btn];
        
        btnFrameNum = btn.frame.size.width+btn.frame.origin.x;
        
        btn.tag = i+1;
        
        [btn addTarget:self action:@selector(commentClick:) forControlEvents:UIControlEventTouchUpInside];
        
        bgView.frame = CGRectMake(0, 64, APPWith, btn.frame.origin.y+50);
    }
    
    for (int i = 0; i < _commentArr.count; i++) {
        
        UIButton *button = (UIButton *)[bgView viewWithTag:[[_commentArr objectAtIndex:i] integerValue]];
        
        button.selected = YES;
        
        button.layer.borderColor = [[UIColor colorWithRed:255.0/255.0 green:59.0/255.0 blue:79.0/255.0 alpha:1]CGColor];
        
        [_commmentStr appendFormat:@"%@;",button.titleLabel.text];
    }
    
    //设计UITextView
    
    UIView *moreView = [[UIView alloc]initWithFrame:CGRectMake(0, bgView.frame.origin.y+bgView.frame.size.height+10, APPWith, 170)];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
    
    
    label.text = @"更多要求";
    
    label.font = [UIFont systemFontOfSize:15];
    
    [moreView addSubview:label];
    
    _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, label.frame.origin.y+label.frame.size.height+10, APPWith, 100)];
    
    _textView.tag = 100;
    
    _textView.layer.borderColor = [[UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:244.0/255.0 alpha:1]CGColor];
    
    _textView.layer.borderWidth = 1;
    
    if ([self.commentDemend length]) {
        
        _textView.text = self.commentDemend;
        _textView.textColor = [UIColor blackColor];
        isFirst = NO;
        textViewValueChange = YES;
    }else {
    
        _textView.text = @"更多要求请在这里写";
        _textView.textColor = [UIColor grayColor];
        isFirst = YES;
        
    }
    
    _textView.delegate = self;
    
    [moreView addSubview:_textView];
    
    [self.view addSubview:moreView];
    
    
    UIButton *OKbtn = [[UIButton alloc]initWithFrame:CGRectMake(20, APPHeight-100, APPWith-40, 50)];
    
    [OKbtn setTitle:@"确定" forState:UIControlStateNormal];
    
    [OKbtn setBackgroundImage:[UIImage imageNamed:@"button1.png"] forState:UIControlStateNormal];
    
    [OKbtn addTarget:self action:@selector(OKBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:OKbtn];
    
}

#pragma mark - 备注按钮的点击事件

- (void)commentClick:(UIButton *)sender {

    sender.selected = !sender.selected;
    
    if (sender.selected) {
        
        sender.layer.borderColor = [[UIColor colorWithRed:255.0/255.0 green:59.0/255.0 blue:79.0/255.0 alpha:1]CGColor];
        
        [self.commmentStr appendFormat:@"%@;",sender.titleLabel.text];
        
        [self.commentArr addObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
        
        NSLog(@"%@",self.commentArr);
    } else {
        
        sender.layer.borderColor = [[UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:244.0/255.0 alpha:1]CGColor];
    
        NSRange range = [self.commmentStr rangeOfString:[NSString stringWithFormat:@"%@;",sender.titleLabel.text]];
        
        [_commmentStr deleteCharactersInRange:NSMakeRange(range.location, range.length)];
       
        [self.commentArr removeObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
        
        
    }


}

#pragma mark - 确定按钮的点击事件
- (void)OKBtnClick {
    
    [self.view endEditing:YES];

    if (textViewValueChange && _textView.text.length) {
        
        [_commmentStr appendFormat:@"%@;",_textView.text];
        
        self.commentDemend = _textView.text;
        
    }
    
    if (!_textView.text.length) {
        self.commentDemend = @"";
    }
    
    
    
    self.Comment(_commmentStr,_commentDemend,_commentArr);
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    [self.view endEditing:YES];

}

- (void)textViewDidBeginEditing:(UITextView *)textView {

    
        
    if (isFirst) {
        
        textView.text = @"";
        
        textViewValueChange = YES;
        
        isFirst = NO;
    }
        
   
    
    textView.textColor = [UIColor blackColor];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backClick:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
